import pymysql

# 数据库配置信息
# "charset": "utf8"
DATABASE = {
    "host": "localhost",
    "database": "db4",
    "user": "root",
    "password": "root"
}

# 连接数据库
# pymysql.connect(
#     host=DATABASE["host"],
#     database=DATABASE["database"],
#     user=DATABASE["user"],
#     password=DATABASE["password"]
# )
conn = pymysql.connect(**DATABASE)

# 插入
# cursor = conn.cursor()
# data = (7, "pig", "666666", 500)
# cursor.execute("INSERT INTO user(id,username,password,money) VALUES(%s,%s,%s,%s)", data)
# conn.commit()
# cursor.close()

# 更新
cursor = conn.cursor()
cursor.execute("delete from user where id = %s", 7)
conn.commit()
cursor.close()

# 查询
cursor = conn.cursor()
cursor.execute("select * from user")
result = cursor.fetchall()
for row in result:
    print(row)
cursor.close()

conn.close()


# 异常处理
try:
    a = 10 / 0
except Exception as e:
    print(e)
